<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Session;
use Captcha;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Spatie\Menu\Laravel\Link;
use Spatie\Menu\Laravel\Menu;

class ConfigController extends commencontroller
{





    ////GET.Admin/config
    function index(){

        $confresoult=DB::table('blog_config')
            ->orderBy('conf_id','desc')
            ->get();
        foreach ($confresoult as $k=>$v){


            switch ($v->field_type) {
                case 'input':



                    $confresoult[$k]->html='<input type="text" class="lg" name="conf_content[]" value="'.$v->conf_content.'"/>';
                 break;
                case 'textarea':
                  $confresoult[$k]->html='<textarea name="conf_content[]">'.$v->conf_content.'</textarea>';
                    break;
                case 'radio':
                    $arrv=explode('，',$v->field_value);


                    $arr='';
                    foreach ($arrv as $m=>$n){
                        $str=explode('|',$n);

                        $c=$v->conf_content==$str[0]?'checked':' ';

                        $arr.='<input type="radio" name="conf_content[]" value="'.$str[0].'"'.$c.'/>'.$str[1];
                    }

                $confresoult[$k]->html=$arr;
echo $arr;
                    break;
            }

        }


        return view('Admin.config.index',compact('confresoult'));

    }


    public function configorder(){
        $input=Input::all();

        $conf_id=$input['conf_id'];
        $conforder= $input['conforder'];
        $confcounte=DB::table('blog_config')
            ->where('conf_id',$conf_id)
            ->update(
                array('conf_order'=>$conforder)
            );

        if ($confcounte>0){
            $date=[
                'status'=>1,
                'msg'=>'修改排序成功'
            ];
        }else{
            $date=[
                'status'=>0,
                'msg'=>'修改失败。请稍后再试'
            ];

        }


        return $date;
    }

//GET|HEAD.Admin/config/create添加
    function create (){
        return view('Admin/config/add');
    }

    //post. Admin/config
    function store (){
        $input=Input::except('_token','conf_id');
        $rules=[
            'conf_name'=>'required',


            'conf_title'=>'required'

        ];
        $message=[
            'conf_name.required'=>'配置名称不能为空',

            'conf_title.required'=>'配置标签不能为空'
        ];
        $validator=Validator::make($input,$rules,$message);

        if($validator->passes()){
            $adresoult=DB::table('blog_config')
                ->insert(
                    $input
                );

            if ($adresoult==true){
                return redirect('Admin/config');
            }else{
                return back()->with('errors','添加失败');
            }

        }else{
            return back()->withErrors($validator);
        }
    }

    //修改
    //GET|HEAD.Admin/config/{index}/edit
    function edit ($conf_id){
        $upresoult=DB::table('blog_config')
            ->where('conf_id',$conf_id)
            ->get();

        return view('Admin/config/update',compact('upresoult'));
    }
    //PUT|PATCH.Admin/config/{index
    function update($conf_id){
        $up=Input::except('_token','_method');
        $upcount=  DB::table('blog_config')
            ->where('conf_id',$conf_id)
            ->update(
                $up
            );

        if ($upcount>0){
            $this->putFile();
            return redirect('Admin/config');
        }else{
            return back()->withErrors('errors',修改失败);
        }

    }
//删除
    //DELETE Admin/config/{index}
    function destroy($conf_id){

        $delcount=DB::table('blog_config')
            ->where('conf_id',$conf_id)
            ->delete();
        if ($delcount<=0){
            $update=[
                'status'=>0,
                'msg'=>'删除失败,请稍后再试',
            ];
        }else{
            $this->putFile();
            $update=[
                'status'=>1,
                'msg'=>'删除成功',
            ];

        }
        return $update;
    }
    //post//不同于一部修改一个一下传一个表单进行修改  方便的多
    public function change(){
        $input=$input=Input::except('_token','order');


        foreach ($input['conf_id'] as $k=>$v){
            $content=DB::table ('blog_config')
                ->where('conf_id',$v)
                ->update(['conf_content'=> $input['conf_content'][$k]]);
        }
        if ($content>0){

            return back()->with('errors','修改失败');
        }else{
            $this->putFile();
            return back()->with('errors','修改成功');
        }

    }

     //通过配置文件修改
        public function putFile(){

             $confresoult=DB::table('blog_config')
                 ->pluck('conf_content','conf_name')
                 ->all();
             $path=base_path().'\config\web.php';

             $str='<?php return '.var_export($confresoult,true).';';
             file_put_contents($path,$str);


        }

    public function Erwei()

    {
        $input=Input::all();

        $lowshu=strtolower($input['captcha']);

        foreach ($input as $m=>$v){
            $v=$lowshu;

        }



         $rules = ['captcha' => 'required|captcha'];
            $validator = Validator::make($input,$rules);

            if ($validator->fails())
            {
                echo '<p style="color: #ff0000;">失败!</p>';
            }
            else
            {
                echo '<p style="color: #00ff30;"> 成功</p>';
            }

    }
    public function getErwei()
    {
        return view('Admin.config.ma');
    }
    //GET|HEAD.Admin/config/{index}
    function show (){
echo '123';
    }

    public function Shao()
    {
       return view('Admin.config.shao');
    }

    public function Test()
    {
    /* $menu = Menu::new()
            ->add(Link::to('/', 'Home'))
            ->add(Link::to('/Admin/config/putfile', 'About'));*/
       /* $menu=Menu::new()
            ->add(Link::to('/', 'Home'))
            ->add(Link::to('/', 'About'))
            ->add(Link::to('/', 'Contact'))
            ->render();*/
        $menu=Menu::new()
            ->addClass('navigation')
        ->add(Link::to('/', 'Home')->addClass('home-link'))
        ->add(Link::to('/about', 'About'))
        ->add(Link::to('/contact', 'Contact')->addParentClass('float-right'))
        ->wrap('div.wrapper');


/*
        $menu= Menu::new()
            ->add(Menu::new()
                ->link('/introduction', 'Introduction')
                ->link('/requirements', 'Requirements')
                ->link('/installation-setup', 'Installation and Setup')
            )
            ->add(Menu::new()
                ->prepend('<h2>Basic Usage</h2>')
                ->prefixLinks('/basic-usage')
                ->link('/your-first-menu', 'Your First Menu')
                ->link('/working-with-items', 'Working With Items')
                ->link('/adding-sub-menus', 'Adding Sub Menus')
            );*/
        return view('Admin.test.test',compact('menu'));
    }

}
